// Functions associated with the Menubar class
//-----------------

//-----------------
// Includes libraries and other files
#include "Menubar.h"
//-----------------

//-----------------
//// CONSTRUCTOR - Menubar
Menubar::Menubar( const int& width)
	: BUTTON_WIDTH( 150), BUTTON_HEIGHT( 30), button_mouseover_( -1)
{
	set_x( 0);
	set_y( 0);
	set_width( width);
	set_height( BUTTON_HEIGHT + 10);

	int x( 5), y( 5);
	for( int i( 0); i < BUTTON_ALL; i++, x+=BUTTON_WIDTH)
		set_button_coordinates( i, x, y);
}

//// DESTRUCTOR - ~Menubar
Menubar::~Menubar() {}
//-----------------

//-----------------
//// FUNCTION - get_button
// return the button number that's currently being cursored over
int Menubar::get_button() const {
	// Pre-conditions :- the button number must be valid (if it isn't then this function shouldn't be called)
	assert( button_mouseover_ >= 0);

	return button_mouseover_;
}

//// FUNCTION - get_button_coordinates
// retrieve the x and y position of the chosen button
void Menubar::get_button_coordinates( const int& b, int& x, int& y) const {
	// Pre-conditions :- button number must be valid
	assert( b <= 0 && b > BUTTON_ALL);

	x = buttons_[b][0];
	y = buttons_[b][1];
}

//// FUNCTION - mouseover_border
// this is the only toolbar that doesn't have a border, so it's an obsolete function
bool Menubar::mouseover_border( const int& x, const int& y) const { return false; }

//// FUNCTION - mouseover_button
// return true if a button is being cursored over
bool Menubar::mouseover_button( const int& x, const int& y) {
	// Pre-conditions :- none
	assert( true);

	// run through all the buttons in the array to test if they're under the mouse position
	for( int i( 0); i < BUTTON_ALL; i++) {
		if(( x >= buttons_[i][0]  && x <= buttons_[i][0]+BUTTON_WIDTH) &&
		   ( y >= buttons_[i][1] && y <= buttons_[i][1]+BUTTON_HEIGHT) ) {
			button_mouseover_ = i;
			return true;
		}
	}

	// if none of the buttons are under the mouse then return false
	button_mouseover_ = -1;
	return false;
}

//// FUNCTION - set_button_coordinates
// set the coordinates of the chosen button
void Menubar::set_button_coordinates( const int& b, const int& x, const int& y) {
	// Pre-conditions :- none
	assert( true);

	if( b < BUTTON_ALL) {
		buttons_[b][0] = x;
		buttons_[b][1] = y;
	}
}

//// FUNCTION - setup_geometry
// create the menubar textures
HRESULT Menubar::setup_geometry( LPDIRECT3DDEVICE9 device) {
	// Pre-conditions :- the device must exist
	assert( device != nullptr);

	HRESULT hr;
	hr = Toolbar::setup_geometry( device);

	hr = hr | D3DXCreateTextureFromFile( device, (PATH_ORTHOGRAPHIC+"texture_toolset_button_new_passive.png").c_str(),		&buttons_textures_[BUTTON_NEW][BUTTON_TEXTURE_PASSIVE]);
	hr = hr | D3DXCreateTextureFromFile( device, (PATH_ORTHOGRAPHIC+"texture_toolset_button_new_mouseover.png").c_str(),	&buttons_textures_[BUTTON_NEW][BUTTON_TEXTURE_MOUSEOVER]);
	hr = hr | D3DXCreateTextureFromFile( device, (PATH_ORTHOGRAPHIC+"texture_toolset_button_load_passive.png").c_str(),		&buttons_textures_[BUTTON_LOAD][BUTTON_TEXTURE_PASSIVE]);
	hr = hr | D3DXCreateTextureFromFile( device, (PATH_ORTHOGRAPHIC+"texture_toolset_button_load_mouseover.png").c_str(),	&buttons_textures_[BUTTON_LOAD][BUTTON_TEXTURE_MOUSEOVER]);
	hr = hr | D3DXCreateTextureFromFile( device, (PATH_ORTHOGRAPHIC+"texture_toolset_button_save_passive.png").c_str(),		&buttons_textures_[BUTTON_SAVE][BUTTON_TEXTURE_PASSIVE]);
	hr = hr | D3DXCreateTextureFromFile( device, (PATH_ORTHOGRAPHIC+"texture_toolset_button_save_mouseover.png").c_str(),	&buttons_textures_[BUTTON_SAVE][BUTTON_TEXTURE_MOUSEOVER]);
	hr = hr | D3DXCreateTextureFromFile( device, (PATH_ORTHOGRAPHIC+"texture_toolset_button_quit_passive.png").c_str(),		&buttons_textures_[BUTTON_QUIT][BUTTON_TEXTURE_PASSIVE]);
	hr = hr | D3DXCreateTextureFromFile( device, (PATH_ORTHOGRAPHIC+"texture_toolset_button_quit_mouseover.png").c_str(),	&buttons_textures_[BUTTON_QUIT][BUTTON_TEXTURE_MOUSEOVER]);

	return hr;
}

//// FUNCTION - render
// output the sidebar to the rendering device
void Menubar::render( LPDIRECT3DDEVICE9 device, const Window& window) {
	// Pre-conditions :- the device must exist
	assert( device != nullptr);

	float x( 0), y( 0), sx( 0), sy( 0);

	// the background images need to be rendered first (i.e. the ones from Toolbar)
	// render the dark background of the menubar
	x  = (get_x() + (get_width()/2)) / window.get_screen_width();
	y  = (window.get_screen_height() - get_y() - (get_height()/2)) / window.get_screen_height();
	sx = get_width() / window.get_screen_width();
	sy = get_height() / window.get_screen_height();
	orthographic_plane_.render( textures_[TEXTURE_BACKGROUND], x, y, 1.5, sx, sy, 1.0, 100);

	// render the buttons
	for( int i( 0); i < BUTTON_ALL; i++) {
		LPDIRECT3DTEXTURE9* temp;

		x  = float( (buttons_[i][0] + (BUTTON_WIDTH/2))) / window.get_screen_width();
		y  = float( window.get_screen_height() - (buttons_[i][1] + (BUTTON_HEIGHT/2))) / window.get_screen_height();
		sx = float( BUTTON_WIDTH) / window.get_screen_width();
		sy = float( BUTTON_HEIGHT) / window.get_screen_height();

		// acquire the correct texture for the button and its current state
		if( i != button_mouseover_)
			temp = &buttons_textures_[i][BUTTON_TEXTURE_PASSIVE];
		else
			temp = &buttons_textures_[i][BUTTON_TEXTURE_MOUSEOVER];

		orthographic_plane_.render( *temp, x, y, 1.6, sx, sy, 1.0, 100);
	}
}

//// FUNCTION - reset
// clear all the user-created data in preparation of a new module
void Menubar::reset() {
	// Pre-conditions :- none
	assert( true);

	button_mouseover_ = -1;
}